package com.example.webmodule.config.strategy;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.example.webmodule.strategy.NacosRegistryStrategy;
import com.example.webmodule.strategy.RegistrySelectStrategy;
import com.example.webmodule.strategy.RegistryStrategyFactory;
import jakarta.annotation.PostConstruct;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@ConditionalOnProperty(name = "spring.cloud.nacos.discovery.enabled", havingValue = "true",
        matchIfMissing = false)
@ConditionalOnClass(NacosDiscoveryProperties.class)
@Configuration
public class NacosRegistryStrategyConfig {

    private static final Logger logger = LoggerFactory.getLogger(NacosRegistryStrategyConfig.class);

    @Autowired
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    @Autowired
    private NacosServiceManager nacosServiceManager;

    @Autowired
    private OkHttpClient okHttpClient;

    @Bean(value="nacosRegistryStrategy")
    public RegistrySelectStrategy nacosRegistryStrategy() {
        if (nacosDiscoveryProperties == null) {
            logger.error("DiscoveryClient is not properly injected, EurekaRegistryStrategy cannot be initialized.");
            throw new IllegalStateException("DiscoveryClient is required for EurekaRegistryStrategy initialization.");
        }
        return new NacosRegistryStrategy(nacosDiscoveryProperties, okHttpClient);
    }

    @Bean
    public RegistryStrategyFactory registryStrategyFactory(
            @Qualifier("nacosRegistryStrategy")RegistrySelectStrategy registrySelectStrategy) {
        if (registrySelectStrategy == null) {
            logger.error("RegistrySelectStrategy is null, RegistryStrategyFactory cannot be initialized.");
            throw new IllegalArgumentException("RegistrySelectStrategy must not be null.");
        }
        logger.info("Initializing RegistryStrategyFactory with provided RegistrySelectStrategy.");

        RegistryStrategyFactory registryStrategyFactory = RegistryStrategyFactory.instance;
        registryStrategyFactory.setRegistryStrategy(registrySelectStrategy);
        return registryStrategyFactory;
    }

    @PostConstruct
    public void init() throws NacosException {
        // 获取 NamingService
        NamingService namingService = nacosServiceManager.getNamingService(
                nacosDiscoveryProperties.getNacosProperties());
        List<String> serviceNameList = namingService.getServicesOfServer(1, Integer.MAX_VALUE).getData();

    }

    private void handleNewServiceInstances(List<Instance> instances) {
        // 处理新服务实例的逻辑
        for (Instance instance : instances) {
            System.out.println("New service instance: " + instance.getIp() + ":" + instance.getPort());
        }
    }
}
